/* 
 * Copyright (C) 2012 Martin Řehánek <Martin.Rehanek at mzk.cz>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */



goog.provide('kramerius.audio.ui.RotatingRecord');

goog.require('goog.dom');
goog.require('goog.Timer');
//goog.require('goog.math');
goog.require('goog.events.EventType');


/**
 * @constructor 
 * @param {Element} container Element containing this component.
 * @param {kramerius.audio.Controller} controller Controller of the audio player.
 * @param {string} recordImageUrl Url of the image of the record.
 * @param {boolean} loggingEnabled whether logging should be enabled (for debugging)
 */
kramerius.audio.ui.RotatingRecord = function(container, controller, recordImageUrl, loggingEnabled){
    kramerius.audio.ui.PlayerComponent.call(this, container, controller);
    this.recordImageUrl_ = recordImageUrl;
    this.loggingEnabled_ = loggingEnabled;
    this.rotating_ = false;
    this.controller_.setRotatingRecord(this);
    this.canvas_ = this.initializeCanvas_();
    this.center_(this.getContext_());
    this.clear_();
    this.draw_();
    this.timer_ = this.initializeTimer_();
};
goog.inherits(kramerius.audio.ui.RotatingRecord, kramerius.audio.ui.PlayerComponent);

/** @private **/
kramerius.audio.ui.RotatingRecord.prototype.initializeCanvas_ = function(){
    var canvas = goog.dom.createDom('canvas',{
        'id':'rotatingRecordCanvas',
        'width':128,  
        'height':128
    });
    var fallbackText = goog.dom.createTextNode("Your web browser doesn't suport HTML5 canvas");
    canvas.appendChild(fallbackText);
    this.container_.appendChild(canvas);
    return canvas;
};

/** @private **/
kramerius.audio.ui.RotatingRecord.prototype.initializeTimer_ = function(){
    var timer = new goog.Timer(50);
    timer.start();
    goog.events.listen(timer, goog.Timer.TICK, this.rotate_, false, this);
    timer.stop();
    return timer;
}

/** @private **/
kramerius.audio.ui.RotatingRecord.prototype.clear_ = function(){
    // console.log("clearing");
    var context = this.getContext_();
    context.clearRect(-this.canvas_.width/2,-this.canvas_.height/2,this.canvas_.width,this.canvas_.height);
}

/** @private **/
kramerius.audio.ui.RotatingRecord.prototype.center_ = function(ctx){
    var centerX = this.canvas_.width / 2;
    var centerY = this.canvas_.height / 2;
    // console.log("center: " + centerX, ", " + centerY);
    ctx.translate(centerX, centerY);
}

/** @private **/
kramerius.audio.ui.RotatingRecord.prototype.rotate_ = function(event){
    //console.log("rotating");
    this.rotationCounter_++;
    this.clear_();
    //context.clearRect(0,0,400,400);
    var context = this.getContext_();
    //var rotation = Math.PI / (14 + random);
    var rotation = - Math.PI / 100;
    //  console.log("rotating " + rotation);
    context.rotate(Math.PI / rotation);
    //this.clear_();
    this.draw_(context);
}

/** @private **/
kramerius.audio.ui.RotatingRecord.prototype.getContext_ = function(){
    //doesn't matter, I check for 2d context support beforeahand
    return this.canvas_.getContext("2d")
}

/** @private **/
kramerius.audio.ui.RotatingRecord.prototype.draw_ = function(){
    // console.log("drawing");
    var context = this.getContext_();
    var imageUrl = this.recordImageUrl_;
    context.drawImage(imageUrl,-64,-64);
}

/** @private **/
kramerius.audio.ui.RotatingRecord.prototype.log_ = function(text){
    if (this.loggingEnabled_){
        console.log("RotatingRecord: " + text);
    }
}


kramerius.audio.ui.RotatingRecord.prototype.startRotating = function(){
    if (this.timer_.enabled){
        this.log_("startRotating: already rotating");
    }else{
        this.timer_.start();
    }
}

kramerius.audio.ui.RotatingRecord.prototype.stopRotating = function(){
    if (!this.timer_.enabled){
        this.log_("stopRotating: already not rotating");
    }else{
        this.timer_.stop();
    }
}